Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Reflection
Imports System.windows.forms
Imports System.CodeDom.Compiler
Imports Microsoft.VisualBasic

Public Class SystemFunction
    Public Shared Function GetFormByName(ByVal formName As String) As Form
        Dim assemblyName As String = [Assembly].GetEntryAssembly().GetName.Name.Replace(" ", "_")
        Dim myType As Type = Type.GetType(assemblyName & "." & formName)
        Return CType(Activator.CreateInstance(myType), Form)
    End Function
    Public Shared Function GetControlByName(ByVal Name As String, _
                                            ByVal CurrentForm As Object) As Control
        If (TypeOf CurrentForm Is Form Or TypeOf CurrentForm Is UserControl) Then
            Dim info As System.Reflection.FieldInfo = _
                        CurrentForm.GetType().GetField("_" & Name, _
                            System.Reflection.BindingFlags.NonPublic Or _
                            System.Reflection.BindingFlags.Instance Or _
                            System.Reflection.BindingFlags.Public Or _
                            System.Reflection.BindingFlags.IgnoreCase)
            If info Is Nothing Then
                Return Nothing
            Else
                Return CType(info.GetValue(CurrentForm), Control)
            End If
        Else
            Return Nothing
        End If
    End Function
    Public Shared Sub ShowMessageForm(ByVal UserMessage As String, ByVal SystemMessage As String)
        Dim frm As New frmMESSAGE
        frm.lblUserMessage.Text = UserMessage
        frm.lblSystemMessage.Text = SystemMessage
        frm.btnOK.Left = (frm.uiPanel0Container.Width - frm.btnOK.Width) / 2
        frm.ShowDialog()
    End Sub
    Public Shared Function ShowYesNoForm(ByVal UserMessage As String, ByVal SystemMessage As String) As Windows.Forms.DialogResult
        Dim frm As New frmMESSAGE
        frm.lblUserMessage.Text = UserMessage
        frm.lblSystemMessage.Text = SystemMessage
        frm.btnMove.Left = (frm.uiPanel0Container.Width - frm.btnMove.Width * 2) / 2
        frm.btnMove.Visible = True
        frm.btnMove.Text = "YES"

        frm.btnJoin.Visible = True
        frm.btnJoin.Text = "NO"
        frm.btnJoin.Left = frm.btnMove.Left + frm.btnMove.Width

        frm.btnOK.Visible = False
        frm.btnClose.Visible = False
        Return (frm.ShowDialog())
    End Function
    Public Shared Function ShowMoveJoinForm(ByVal UserMessage As String, ByVal SystemMessage As String) As Windows.Forms.DialogResult
        Dim frm As New frmMESSAGE

        frm.lblUserMessage.Text = UserMessage
        frm.lblSystemMessage.Text = SystemMessage

        frm.btnMove.Left = (frm.uiPanel0Container.Width - frm.btnMove.Width * 3) / 2
        frm.btnMove.Visible = True

        frm.btnJoin.Visible = True
        frm.btnJoin.Left = frm.btnMove.Left + frm.btnMove.Width

        frm.btnClose.Left = frm.btnJoin.Left + frm.btnJoin.Width
        frm.btnClose.Visible = True

        frm.btnOK.Visible = False
        Return (frm.ShowDialog())
    End Function
    Public Shared Function ReadAtribute(ByVal str As String) As DataTable
        Try
            Dim dataSet As DataSet = New DataSet
            Dim dataTable As DataTable = New DataTable("table1")
            dataTable.Columns.Add("col1", Type.GetType("System.String"))
            dataTable.Columns.Add("col2", Type.GetType("System.String"))
            dataSet.Tables.Add(dataTable)

            Dim xmlData As String = str

            Dim xmlSR As System.IO.StringReader = New System.IO.StringReader(xmlData)

            dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema)

            Return dataSet.Tables(0)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Sub Update_Language(ByVal FormName As String, _
    ByVal ControlName As String, ByVal SubControlName As String, _
    ByVal Translate As String, ByVal Origin As String)

        Try
            If PublicFunction.OpenSQLConnection() Then
                Dim MyCommand As SqlCommand = New SqlCommand("LANG_UPDATE", PublicVariable.Connection)
                MyCommand.CommandType = CommandType.StoredProcedure
                MyCommand.Parameters.AddWithValue("@FormName", FormName)
                MyCommand.Parameters.AddWithValue("@ControlName", ControlName)
                MyCommand.Parameters.AddWithValue("@SubControlName", SubControlName)
                MyCommand.Parameters.AddWithValue("@Translate", Translate)
                MyCommand.Parameters.AddWithValue("@LangCode", PublicVariable.LangCode)
                MyCommand.Parameters.AddWithValue("@Origin", Origin)
                MyCommand.ExecuteNonQuery()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
   
    Public Shared Sub WriteLog(ByVal Str As String)
        Dim oWrite As IO.StreamWriter
        Dim FilePath As String
        FilePath = Application.StartupPath + "www.TOTAL-POS.net.logo"

        If IO.File.Exists(FilePath) Then
            oWrite = IO.File.AppendText(FilePath)
        Else
            oWrite = IO.File.CreateText(FilePath)
        End If
        oWrite.Write(Now.ToString() + ":" + Str + vbCrLf)
        oWrite.Close()
    End Sub
    
End Class
